package com.windoent.yugioh.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.windoent.yugioh.R;

/**
 * author : robert
 * date   : 2021/4/28 11:23
 * desc   :
 */
public class GameTypeMeInfoTitle extends RelativeLayout {
    private Context mConText;
    private TextView mTvName;
    private LinearLayout mLlParent;
    private TextView mTvNameOpen;
    private ImageView mIvArrow;
    private String name;
    private boolean clickOpen = false;

    public GameTypeMeInfoTitle(Context context) {
        this(context, null, 0);
    }

    public GameTypeMeInfoTitle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GameTypeMeInfoTitle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mConText = context;
        LayoutInflater.from(mConText).inflate(R.layout.custom_view_game_type_meinfo_title, this);
        TypedArray attributes = mConText.obtainStyledAttributes(attrs, R.styleable.GameTypeMeInfoNumer, 0, 0);
        try {
            name = attributes.getString(R.styleable.GameTypeMeInfoNumer_name);
        } finally {
            attributes.recycle();
        }
        setUpView();
    }

    private void setUpView() {
        mTvName = (TextView) findViewById(R.id.tv_name);
        mLlParent = (LinearLayout) findViewById(R.id.ll_parent);
        mTvNameOpen = (TextView) findViewById(R.id.tv_name_open);
        mIvArrow = (ImageView) findViewById(R.id.iv_arrow);

        mTvName.setText(name);
        mLlParent.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                clickOpen = !clickOpen;
                if (clickOpen) {
                    //点击展开
                    ObjectAnimator animator = ObjectAnimator.ofFloat(mIvArrow, "rotation", 0f, -180f);
                    animator.setDuration(300);
                    animator.start();
                    mTvNameOpen.setText(R.string.string_click_close_warn);
                    if (listener != null) {
                        listener.onClick(true);
                    }
                } else {
                    //点击关闭
                    ObjectAnimator animator = ObjectAnimator.ofFloat(mIvArrow, "rotation", -180f, 0f);
                    animator.setDuration(300);
                    animator.start();
                    mTvNameOpen.setText(R.string.string_click_open_warn);
                    if (listener != null) {
                        listener.onClick(false);
                    }
                }
            }
        });
    }

    public void setNumber(int number) {
        if (number == 1) {
            mLlParent.setVisibility(GONE);
        } else {
            mLlParent.setVisibility(VISIBLE);
        }
        if (clickOpen) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(mIvArrow, "rotation", -180f, 0f);
            animator.setDuration(300);
            animator.start();
            mTvNameOpen.setText(R.string.string_click_open_warn);
            clickOpen = false;
        }
    }

    public void setText(String s) {
        name = s;
        mTvName.setText(name);
    }

    //接口回调
    public interface onClickListener {
        void onClick(boolean open);
    }

    onClickListener listener;

    public void setClickListener(onClickListener listener) {
        this.listener = listener;
    }
}
